package com.aixin.project.servlet;

import com.aixin.project.data.*;
import com.aixin.project.handler.CourseHandler;
import com.aixin.project.handler.CourseOrderHandler;
import com.aixin.project.util.RequestUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.velocity.Template;
import org.apache.velocity.context.Context;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by IntelliJ IDEA.
 * User: Sufeng
 * Date: 12-5-2
 * Time: 下午11:46
 * To change this template use File | Settings | File Templates.
 */
public class LiveBroadcastServlet extends BaseServlet {
    @Override
    protected Template execute(HttpServletRequest request, HttpServletResponse response, Context ctx) {
        AxSession axSession = (AxSession) request.getSession().getAttribute(CommonConstants.USER_SESSION);
        if (axSession == null) {
            try {
                response.sendRedirect("/login");
                return null;
            } catch (IOException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
        }

        long courseId = RequestUtil.getLong(request, "c", -1);
        if (courseId != -1) {
            Course course = CourseHandler.find(courseId);
            if (course != null) {
                if (axSession.getUserType().equals(UserType.USER)) {
                    CourseOrder co = CourseOrderHandler.find(course.getId(), axSession.getUid());
                    if (co != null && co.getStatus() == 1) { //已支付
                        if (StringUtils.trimToNull(course.getWeburl()) == null) {
                            ctx.put("ERROR", "该课程还未开始，请稍后重试！");
                        }else{
                            String weburl = course.secretWeburl();
                            if("full".equals(weburl)){
                                ctx.put("ERROR", "对不起，您的登陆点已达到上限，不能进入该课程！");
                            }else if(weburl == null){
                                ctx.put("ERROR", "该课程还未开始，请稍后重试！");
                            }else{
                                ctx.put("course", course);
                                ctx.put("weburl", weburl);
                            }
                        }
                    } else {
                        ctx.put("ERROR", "您还未订购本课程,如果还想观看，请联系我们客服人员，联系电话 "+CommonConstants.SERVICETEL);
                    }
                } else if (axSession.getUserType().equals(UserType.EXPERT)) {
                    if (course.getExpertid() == axSession.getUid()) {

                        if (StringUtils.trimToNull(course.getClienturl()) == null) {
                            ctx.put("ERROR", "该课程还未开始，请稍后重试！");
                        } else {
                            ctx.put("course", course);
                        }
                    } else {
                        ctx.put("ERROR", "对不起，您不能进入该课程！");
                    }

                }
            }
        }
        return getTemplate("livebroadcast.vm");
    }
}
